﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Quaack.Data;
using Quaack.Web.Controls;
using System.Web.Security;

namespace Quaack.Web.Master
{
    public partial class Defaut : System.Web.UI.MasterPage
    {

        public TextBox TxtZoeken
        {
            get
            {
                return _txtZoeken;
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            // CreateMessages();
            // CreateUsers();

            SetCurrentUserPage();

            // Indien ingelogd
            if (Page.User.Identity.IsAuthenticated)
            {
                Provider.CurrentUser = Profiel.FromGebruikersnaam(Page.User.Identity.Name);
                if (Provider.CurrentUser.IsGeblokkeerd)
                {
                    // Account is geblokkeerd terwijl de gebruiker ingelogd was, uitloggen
                    FormsAuthentication.SignOut();
                    Response.Redirect(Page.ResolveUrl("~/Default.aspx"));
                }

                var currentUser = (CombinedContentHolder)Page.LoadControl("~/Controls/CombinedContentHolder.ascx");
                currentUser.Profiel = Provider.CurrentUser;

                // Indien niet de Gebruikers home page
                if (!Provider.IsHomePage)
                    currentUser.ShowContent = false;


                AddContentHolderToTable(currentUser, CurrentUserUserId);

            }

            // zijn we op een gebruikerspagina?
            if (!Provider.IsHomePage && Provider.CurrentUserPage != null)
            {
                var currentUserPage = (CombinedContentHolder)Page.LoadControl("~/Controls/CombinedContentHolder.ascx");
                currentUserPage.Profiel = Provider.CurrentUserPage;
                AddContentHolderToTable(currentUserPage, CurrentUserPageUserId);
            }

            if (CurrentUserUserId.Rows.Count > 0 && CurrentUserPageUserId.Rows.Count > 0)
            {
                BadgeHolder.CssClass = "badge-holder has-both";
            }
        }

        void AddContentHolderToTable(CombinedContentHolder profiel, Table table)
        {
            table.Rows.Clear();

            if (profiel != null)
            {

                TableRow rowToAdd = new TableRow();
                var cell = new TableCell();
                cell.Controls.Add(profiel);
                rowToAdd.Cells.Add(cell);

                table.Rows.Add(rowToAdd);
            }
        }

        void SetCurrentUserPage()
        {
            // Id uit url halen, indien geen getal is dit mislukt
            string idString = Request.QueryString["profiel"];
           
            Profiel profiel = null;
            try
            {
                Provider.CurrentUserPage = Profiel.FromId(int.Parse(idString));
            }
            catch (Exception)
            {
                Provider.CurrentUserPage = null;
            }
        }

        void CreateUsers()
        {
            for (int i = 0; i < 20; i++)
            {
                Profiel user = new Profiel("Peter" + i, "bla bla" + i, "peter@peter.nl", "123456123", "");
            }
        }

        void CreateMessages()
        {
            foreach (Profiel user in Profiel.Zoeken(""))
            {



                for (int i = 0; i < 5; i++)
                {
                    Bericht message = new Bericht(user, "bericht, bericht.. " + i);
                }
            }
        }

    }
}